/**
 * Created by wzq on 15-4-17.
 */

define([], function () {
    var ViewClassLoader = {};

    function loadHtml(path, callback) {
        require(['text!' + path], function (tpl) {
            var ViewClass = Backbone.View.extend({
                render: function () {
                    var tmpl = _.template(tpl);
                    $(this.el).html(tmpl);
                    return this;
                },

                onRender: function (qs, callback) {
                    this.render();
                    if (callback)
                        callback(this);
                },

                onReshow: function (qs, callback, forceReload) {
                    $(this.el).show();
                },

                hide: function () {
                    $(this.el).hide();
                },

                remove: function () {
                    $(this.el).remove();
                }
            });
            callback(ViewClass);
        });
    }

    ViewClassLoader.getViewClass = function (path, callback) {
        if (path.endsWith('.html')) {
            loadHtml(path, callback);
        } else {
            require([path], function (ViewClass) {
                callback(ViewClass);
            });
        }
    };

    return ViewClassLoader;
});